import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Test from '@/components/test'
// import Home from '@/views/home'
import NewsDetail from '@/views/NewsDetail'
// import Users from '@/views/Users'
import UserAdd from '@/views/UserAdd'
import Login from '@/views/Login'
import Mine from '@/views/Mine'
import store from '../store'

Vue.use(Router)

const router=new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component:()=>import('@/views/home')
    },
    {
      path: '/newsdetail/:id',
      name: 'Newsdetail',
      component: NewsDetail
    },
    {
      path: '/users',
      name: 'Users',
      component:()=>import('@/views/Users')
    },
    {
      path: '/useradd',
      name: 'Useradd',
      component: UserAdd
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/mine',
      name: 'mine',
      component: Mine
    },
    {
      path: '/hello',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/test',
      name: 'Test',
      component: Test
    },    
  ]
})

router.beforeEach((to, from, next) => {
  if (to.name !== 'login' && !store.state.isLogin) next({ name: 'login' })
  else next()
})

export default router;